package com.ifast.common.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.domain.LogDO;
import com.ifast.common.service.LogService;
import com.ifast.common.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <pre>
 * 日志
 * </pre>
 * 
 * <small> 2018年3月23日 | Aron</small>
 */
@RequestMapping("/common/log")
@Controller
public class LogController extends AdminBaseController {
    @Autowired
    LogService logService;
    String prefix = "common/log";
    
    @Log("进入系统日志列表页面")
    @GetMapping()
    String log() {
        return prefix + "/log";
    }
    
    @Log("查询系统日志列表")
    @ResponseBody
    @GetMapping("/list")
    public Result<Page<LogDO>> list(HttpServletRequest request){
        String operation = request.getParameter("operation");
        String searchName = request.getParameter("searchName");

        // 查询列表数据
        Wrapper<LogDO> wrapper = new EntityWrapper<LogDO>();
        wrapper.and("1=1");
        if(StringUtils.isNotBlank(operation)){
            wrapper.eq("operation",operation);
        }

        if(StringUtils.isNotBlank(searchName)){
            wrapper.like("concat(username,operation)",searchName);//多字段模糊匹配
        }

        String sortName = request.getParameter("sortName");
        String sortOrder = request.getParameter("sortOrder");
        if(StringUtils.isNotBlank(sortName)){
            if("ssqxView".equals(sortName)){
                wrapper.orderBy("ssqx","asc".equals(sortOrder));
            }else if("xxlxView".equals(sortName)){
                wrapper.orderBy("xxlx","asc".equals(sortOrder));
            }else if("xxlbView".equals(sortName)){
                wrapper.orderBy("xxlb","asc".equals(sortOrder));
            }else{
                wrapper.orderBy(sortName,"asc".equals(sortOrder));
            }
        }else{
            wrapper.orderBy("gmt_create",false);
        }


        Page<LogDO> page = logService.selectPage(getPage(LogDO.class), wrapper);
        return Result.ok(page);
    }
    
    @Log("删除系统日志")
    @ResponseBody
    @PostMapping("/remove")
    Result<String> remove(Long id) {
        logService.deleteById(id);
        return Result.ok();
    }
    
    @Log("批量删除系统日志")
    @ResponseBody
    @PostMapping("/batchRemove")
    Result<String> batchRemove(@RequestParam("ids[]") Long[] ids) {
        logService.deleteBatchIds(Arrays.asList(ids));
        return Result.fail();
    }
}
